package org.example.contract.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.example.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.contract.domain.Contract;
import org.example.contract.service.IContractService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-11
 */
@Controller
@RequestMapping("/contract/contract")
public class ContractController extends BaseController
{
    private String prefix = "contract/contract";

    @Autowired
    private IContractService contractService;

    @GetMapping()
    public String contract(String contractType,String roleFlag ,ModelMap mmp)
    {
        mmp.put("contractType",contractType);
        mmp.put("roleFlag",roleFlag);
        return prefix + "/contract";
    }

//    @RequiresPermissions("contract:contract:view")
//    @GetMapping("/sellContract")
//    public String sellContract(String contractType,String roleFlag ,ModelMap mmp)
//    {
//        mmp.put("contractType",contractType);
//        mmp.put("roleFlag",roleFlag);
//        return prefix + "/sellContract";
//    }

    /**
     * 查询合同管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表
     */
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Contract contract)
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        return util.exportExcel(list, "合同管理数据");
    }

    /**
     * 新增合同管理
     */
    @GetMapping("/add")
    public String add(String contractType,ModelMap mmp)
    {
        mmp.put("contractType",contractType);
        return prefix + "/add";
    }

    /**
     * 新增保存合同管理
     */
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Contract contract)
    {
        // 合同编号
       // String contractCode= getContractCode(contract.getContractType());
       // contract.setContractCode(contractCode);
        Integer i=contractService.insertContract(contract);

        return toAjax(i);
    }

    /**
     * 合同编号
     * @return
     */
    private String getContractCode(String contractType) {
        Contract contract =new Contract();
        contract.setCreateTime(DateUtils.getNowDate());
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal()+1L;
        String s2 = String.format("%04d", total);
        String contractCode = null;
        if(Util.CONTRACT_TYPE_SELL.equals(contractType)){
            //销售
            contractCode =  "SO"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }else{
            //采购
            contractCode =  "PO"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }
        return contractCode;
    }

    /**
     * 修改合同管理
     */
    @GetMapping("/edit/{contractId}")
    public String edit(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        Contract contract = contractService.selectContractByContractId(contractId);
        mmap.put("contract", contract);
        return prefix + "/edit";
    }

    @GetMapping("/detail/{contractId}")
    public String detail(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        Contract contract = contractService.selectContractByContractId(contractId);
        mmap.put("contract", contract);
        return prefix + "/detail";
    }

    /**
     * 修改保存合同管理
     */
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Contract contract)
    {
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除合同管理
     */
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contractService.deleteContractByContractIds(ids));
    }
}
